/**
 * Created by 静龄 on 15-11-12.
 */

!
    function(){
        var warningpage=new Rpage.pageObj();
        Rswitch.setswitch($(".R_switch"));

        function getdata(issavecheckbox){
            if(issavecheckbox){
                var checkarray=[];
                $(".warningtable").children("tbody").children("tr").each(function(index,e){
                    if($(this).children("td:eq(0)").children("input")[0].checked) {
                        checkarray.push(index);
                    }
                });

            }else{
                $(".task_list").html("");
                $.post(TASK_LIST,{"type":"0"},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        var datalength=data.result.length;
                        for(var i=0;i<datalength;i++){
                            $(".task_list").append('' +
                                '<div class="checkbox">' +
                                '   <label>' +
                                '       <input type="checkbox" value="'+data.result[i].id+'" data-show="'+data.result[i].task_name+'">' +data.result[i].task_name+
                                '   </label>' +
                                '</div>');
                        }
                    }
                });
                $(".allcheck")[0].checked=false;
            }
            $.post(ALARM_SETTING_LIST,{"page":warningpage.pagenum,"pageCount":warningpage.perpage},function(data){
                cleantable($(".warningtable"));
                data= $.parseJSON(data);
                if(data.code==1){
                    var datalength=data.result.length;
                    for(var i=0;i<datalength;i++){
                        var tdstr='<tr data-id="'+data.result[i].id+'" data-rule-alert="'+data.result[i].rule_alert+'" data-alarm-way="'+data.result[i].alarm_way+'">' +
                            '<td><input type="checkbox"></td>' +
                            '<td class="table_keep">'+(data.result[i].enabled=="1"?"启用":"未启用")+'</td>' +
                            '<td>'+data.result[i].alarm_name+'</td>' +
                            '<td class="table_keep">'+(function(data,i){
                                var namestr="";
                                if(data.result[i].task_name!=null){
                                    var namelength=data.result[i].task_name.length;
                                    for(var j=0;j<namelength;j++){

                                        namestr=namestr+data.result[i].task_name[j];
                                        if(j!=(namelength-1)){
                                            namestr=namestr+",";
                                        }
                                    }
                                }
                                return namestr;
                            })(data,i)+'</td>' +
                            '<td>'+(function(data,i){
                                switch (data.result[i].alarm_way) {
                                    case "0":
                                        return "声音，邮件";
                                        break;
                                    case "1":
                                        return "声音";
                                        break;
                                    case "2":
                                        return "邮件";
                                        break;
                                    default :
                                        return "不报警";
                                        break;
                                }
                            })(data,i)+'</td>' +
                            '<td class="text-center tablepp"><i class="fa fa-'+(data.result[i].enabled==0?"play":"pause")+'"></i></td>' +
                            '<td><i class="fa fa-edit"></i><i data-toggle="modal" data-target="#myModal"  class="fa fa-trash"></i></td>' +
                            '</tr>';
                        $(".warningtable").children("tbody").append(tdstr);
                    }
                    if(issavecheckbox){
                        $(".warningtable").children("tbody").children("tr").each(function(index,e){
                            var checkarraylength=checkarray.length;
                            for(var i=0;i<checkarraylength;i++){
                                if(checkarray[i]==index){
                                    $(this).children("td:eq(0)").children("input")[0].checked=true;
                                }
                            }
                        });
                    }
                    warningpage.setallpagenum(data.count);
                }
            });



        }
        getdata();
        warningpage.setpagediv($(".table_page"));
        warningpage.setpagechange(function(e){
            getdata();
        });


        $(".warningtable").children("tbody").on("click","tr td i",function(e){
            var trindex=$(this).parent().parent().index();
            switch ($(this).index('.warningtable tbody tr:eq('+trindex+') td i ')){
                case 1:
                    $(".openRwindow").click();
                    $(".R_windows_title").html("修改报警设置");
                    $(".alarm_name input").val($(this).parent().parent().children("td:eq(2)").html());
                    $(".alarm_rule textarea").val($(this).parent().parent().attr("data-rule-alert"));

                    var task_list=$(this).parent().parent().children("td:eq(3)").html().split(",");
                    console.log(task_list);
                    $(".task_list .checkbox input").each(function(index,e){
                        var task_listlength=task_list.length;
                        for(var i=0;i<task_listlength;i++){
                            console.log($(this).attr("data-show"));
                            if($(this).attr("data-show")==task_list[i]){
                                $(this)[0].checked=true;
                            }
                        }
                    });
                    $(".R_switch").attr("data-ison",($(this).parent().parent().children("td:eq(5)").children().hasClass("fa-pause")?"true":"false"));
                    Rswitch.setswitch($(".R_switch"));

                    var alarmway=$(this).parent().parent().attr("data-alarm-way");
                    $(".alarm_sound")[0].checked=false;
                    $(".alarm_email")[0].checked=false;
                    switch (alarmway) {
                        case "0":
                            $(".alarm_sound")[0].checked=true;
                            $(".alarm_email")[0].checked=true;
                            break;
                        case "1":
                            $(".alarm_sound")[0].checked=true;
                            $(".alarm_email")[0].checked=false;
                            break;
                        case "2":
                            $(".alarm_sound")[0].checked=false;
                            $(".alarm_email")[0].checked=true;
                            break;
                        default :
                            $(".alarm_sound")[0].checked=false;
                            $(".alarm_email")[0].checked=false;
                            break;
                    }


                    $(".openRwindow").attr("data-id",$(this).parent().parent().attr("data-id"));
                    break;
                case 0:
                    $.post(UPDATE_ALARM_SETTING,{"id":$(this).parent().parent().attr("data-id"),"enabled":($(this).hasClass("fa-pause")?"0":"1")},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                           getdata();
                        }else{
                            alert(data.result);
                        }
                    });

                    break;
                case 2:
                    $(".delete_yes").attr("data-isall","no");
                    $(".delete_yes").attr("data-id",$(this).parent().parent().attr("data-id"));
                    //$.post(DELETE_TASK,{"id":$(this).parent().parent().attr("data-id")},function(data){
                    //    data= $.parseJSON(data);
                    //    if(data.code==1){
                    //        thistr.remove();
                    //        alert(data.result);
                    //    }else{
                    //        alert(data.result);
                    //    }
                    //});
                    break;
                default :
                    break;
            }
        });
        $(document).ready(function(e){
            $(".delete_yes").click(function(e){
                if($(".delete_yes").attr("data-isall")=="no"){
                    $.post(DELETE_ALARM_SETTING,{"id":$(this).attr("data-id")},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                            alert(data.result);
                        }else{
                            alert(data.result);
                        }
                    });
                }else{
                    $(".warningtable").children("tbody").children("tr").each(function(index,e){
                        if($(this).children("td:eq(0)").children("input")[0].checked){
                            console.log($(DELETE_ALARM_SETTING).attr("data-id"));
                            $.post(DELETE_ALARM_SETTING,{"id":$(this).attr("data-id")},function(data){
                                data= $.parseJSON(data);
                                if(data.code==1){
                                    getdata();
                                }else{
                                    alert(data.result);
                                }
                            });
                        }
                    });
                    $(".allcheck")[0].checked=false;
                }
            });
        });


        $(".warningsetting_ok").click(function(e){

            if(Rcheck.checknull($(".alarm_name input").val())){
                alert("报警名不得为空");
                return false;
            }else if(Rcheck.checklength($(".alarm_name input").val(),16)){
                alert("报警名长度不得超过16个字符");
                return false;
            }else if(Rcheck.checknull($(".alarm_rule textarea").val())){
                alert("关键词不得为空");
                return false;
            }else if(Rcheck.checklength($(".alarm_rule textarea").val(),255)){
                alert("关键词长度不得超过255个字符");
                return false;
            }else if($(".task_list").val()==null){
                alert("匹配任务不得为空");
                return false;
            }
            var taskid="";
            $(".task_list .checkbox input").each(function(index,e){
                if($(this)[0].checked){
                    if(taskid==""){
                        taskid=taskid+$(this).val();
                    }else{
                        taskid=taskid+",";
                        taskid=taskid+$(this).val();
                    }
                }
            });





            if($(".R_windows_title").html()=="添加报警"){

                var alarm_way="";
                if((!$(".alarm_sound")[0].checked)&&(!$(".alarm_email")[0].checked)){
                    alert("至少选择一种报警方式")
                    return false;
                }
                if($(".alarm_sound")[0].checked){
                    alarm_way=1;
                    if($(".alarm_email")[0].checked){
                        alarm_way=0;
                    }
                }else{
                    if($(".alarm_email")[0].checked){
                        alarm_way=2;
                    }
                }


                $.post(ADD_ALARM_SETTING,{"alarm_name":$(".alarm_name input").val(),"rule_alert":$(".alarm_rule textarea").val(),"task_id":taskid,"enabled":($(".R_switch").attr("data-ison")=="true"?1:0),"alarm_way":alarm_way},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        getdata();
                        alert("添加成功");
                        $(".R_windows").hide();
                    }else{
                        alert(data.result);
                    }
                });
            }else{
                var alarm_way="";
                if((!$(".alarm_sound")[0].checked)&&(!$(".alarm_email")[0].checked)){
                    alert("至少选择一种报警方式")
                    return false;
                }
                if($(".alarm_sound")[0].checked){
                    alarm_way=1;
                    if($(".alarm_email")[0].checked){
                        alarm_way=0;
                    }
                }else{
                    if($(".alarm_email")[0].checked){
                        alarm_way=2;
                    }
                }

                $.post(UPDATE_ALARM_SETTING,{"id":$(".openRwindow").attr("data-id"),"alarm_name":$(".alarm_name input").val(),"rule_alert":$(".alarm_rule textarea").val(),"task_id":taskid,"enabled":($(".R_switch").attr("data-ison")=="true"?1:0),"alarm_way":alarm_way},function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        getdata();
                        alert("修改成功");
                        $(".R_windows").hide();
                    }else{
                        alert(data.result);
                    }
                });
            }
        });





        $(".allcheck").click(function(e){
            $(".warningtable").children("tbody").children("tr").each(function(index,e){
                $(this).children("td:eq(0)").children("input")[0].checked= $(".allcheck")[0].checked;
            });
        });


        $(".alldelete").click(function(e){
            $(".delete_yes").attr("data-isall","yes");
        });



        $(".allstart").click(function(e){

            $(".warningtable").children("tbody").children("tr").each(function(index,e){
                if($(this).children("td:eq(0)").children("input")[0].checked){
                    var thistr=$(this);
                    $.post(UPDATE_ALARM_SETTING,{"id":$(this).attr("data-id"),"enabled":"1"},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                        }else{
                            alert(data.result);
                        }
                    });
                }
            });
            $(".allcheck")[0].checked=false;
        });

        $(".allstop").click(function(e){
            $(".warningtable").children("tbody").children("tr").each(function(index,e){
                if($(this).children("td:eq(0)").children("input")[0].checked){
                    var thistr=$(this);
                    $.post(UPDATE_ALARM_SETTING,{"id":$(this).attr("data-id"),"enabled":"0"},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                        }else{
                            alert(data.result);
                        }
                    });
                }
            });
            $(".allcheck")[0].checked=false;
        });


        window.setInterval(function(){
            getdata(true);
        },3000);







    }();